<?
class Authlogin
{
	var $CI;
	var $user_table = 'usr_login';
	var $admin_path = '/login';
/*
--------------------------------------------------------------------------------
*/
	function Authlogin()
	{
		$this->CI =& get_instance();
			
	}
/*
--------------------------------------------------------------------------------
*/	
	function isLoggedin($user) {
		return ($this->CI->session->userdata('USR_LOGINS') == $user
				   && $this->CI->session->userdata('logged_in'));
	}	
/*
--------------------------------------------------------------------------------
*/
	function chkLoggedin($user) {
		if (!$this->CI->authlogin->isLoggedin($user)){
			$this->logout();
			$this->CI->session->set_flashdata('Forced Logout');
			redirect($this->admin_path);
		};
	}
/*
--------------------------------------------------------------------------------
*/	
	function login($user = '', $password = '') {
		if($user == '' OR $password == '') {
			return false;
		}
		if ($this->isLoggedin($user)){
		$this->logout();
			return false;
		};
		$this->CI->db->where('usr_name', $user);
		$query = $this->CI->db->get_where($this->user_table);
		//echo $this->CI->db->last_query();
		if ($query->num_rows() > 0) {
			$row = $query->row_array(); 
			if(md5($password) == $row['usr_password']) {
				$this->CI->session->sess_destroy();
				$this->CI->session->sess_create();
				unset($row['usr_password']);
				$this->CI->session->set_userdata($row);
				//echo($this->CI->session->userdata('USR_LOGINS'));
				//return;
				$this->CI->session->set_userdata(array('logged_in' => true));
				return true;
			}
      else {
          return false;
      }
		} else {
			return false;
		}	
	}	
/*
--------------------------------------------------------------------------------
*/
	function logout() {
		$this->CI->session->sess_destroy();
	}

}
?>